﻿@page "/gaussianBlur"
@using VectSharp;
@using VectSharp.SVG;
@using VectSharp.Filters;
@using VectSharp.Raster.ImageSharp;

<style>
	table {
		table-layout: fixed;
	}

	td {
		width: 400px;
		padding: 10px;
	}
</style>

<div style="width: 100vw; height: 100vh; position: relative;">
	<div style="width: calc(100% - 400px); height: 100%; position: absolute; top: 0; left: 0; text-align: center">
		<img src="@imgSource" style="max-width: 100%; max-height: 100%; margin-top: 50vh; transform: translate(0, -50%)" />
	</div>

	<div style="width: 400px; height: 100vh; position: absolute; top: 0; right: 0;">
		<table style="margin-top: 50vh; transform: translate(0, -50%)">
			<tr>
				<td style="text-align: center">
					<MatNumericUpDownField Label="Standard deviation"
										   @bind-Value=@stdDev
										   Step="0.1"
										   Minimum="0"
										   DecimalPlaces="2">
					</MatNumericUpDownField>
				</td>
			</tr>

		</table>
	</div>
</div>

@code {
	private double _stdDev = 1;
	private double stdDev
	{
		get
		{
			return _stdDev;
		}

		set
		{
			_stdDev = value;
			Render();
		}
	}

	private string imgSource = "";

	protected override Task OnInitializedAsync()
	{
		Render();
		return Task.CompletedTask;
	}

	public void Render()
	{
		Page page = new Page(100, 100);
		Graphics graphics = page.Graphics;

		// Graphics object containing the elements to which the filter will be applied.
		Graphics filterSubject = new Graphics();

		// Draw some text that will be blurred.
		Font font = new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.HelveticaBold), 15);
		filterSubject.FillText(15, 40, "VectSharp", font, Colours.Black);

		// Standard deviation for the Gaussian blur.
		double standardDeviation = stdDev;

		// Create the GaussianBlurFilter object.
		IFilter filter = new GaussianBlurFilter(standardDeviation);

		// Draw the filterSubject on the graphics, using the filter.
		graphics.DrawGraphics(0, 0, filterSubject, filter);

		using (MemoryStream ms = new MemoryStream())
		{
			page.SaveAsSVG(ms);
			ms.Seek(0, SeekOrigin.Begin);

			using (StreamReader sr = new StreamReader(ms))
			{
				this.imgSource = "data:image/svg+xml;base64," + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sr.ReadToEnd()));
			}
		}
	}
}
